package org.chs.ts.batch.threadservice;


import lombok.Getter;
import lombok.Setter;


import org.chs.ts.base.batch.vo.entity.BatchExecReq;
import org.chs.ts.base.batch.vo.entity.BatchExecResp;
import org.chs.ts.base.constant.CommonConstant;
import org.chs.ts.batch.util.ConvertToUtil;
import org.chs.ts.batch.exception.TaskExecException;
import org.chs.ts.batch.exception.TaskStepExecException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2023/1/9 14:11
 * @Modify 2023/1/9 14:11
 * @Description
 */
@Getter
@Setter
public abstract class BatchServiceBase<E, R> implements Callable<BatchExecResp> {


    private long taskStartTime;

    private long taskEndTime;


    private long taskStepStartTime;


    private long taskStepEndTime;

    private String path;

    private String ip;

    private String port;

    //    public void setTaskStepStartTime(long taskStepStartTime) {
//        this.taskStepStartTime = taskStepStartTime;
//    }

    //    public void setTaskStepEndTime(long taskStepEndTime) {
//        this.taskStepEndTime = taskStepEndTime;
//    }


//    public long getTaskStartTime() {
//        return taskStartTime;
//    }

//    public void setTaskStartTime(long taskStartTime) {
//        this.taskStartTime = taskStartTime;
//    }

//    public long getTaskEndTime() {
//        return taskEndTime;
//    }

//    public void setTaskEndTime(long taskEndTime) {
//        this.taskEndTime = taskEndTime;
//    }

//    public String getPath() {
//        //StringUtils.isBlank()
//        return path;
//    }

//    public void setPath(String path) {
//        this.path = path;
//    }

//    public String getIp() {
//        return ip;
//    }

//    public void setIp(String ip) {
//        this.ip = ip;
//    }
//
//    public String getPort() {
//        return port;
//    }
//
//    public void setPort(String port) {
//        this.port = port;
//    }

    private E taskObject;

//    public E getTaskObject() {
//        return taskObject;
//    }
//
//    public void setTaskObject(E taskObject) {
//        this.taskObject = taskObject;
//    }

    private List<R> taskStepListObject;

//    public List<R> getTaskStepListObject() {
//        return taskStepListObject;
//    }
//
//    public void setTaskStepListObject(List<R> taskStepListObject) {
//        this.taskStepListObject = taskStepListObject;
//    }

    private R taskStepObject;

//    public R getTaskStepObject() {
//        return taskStepObject;
//    }
//
//    public void setTaskStepObject(R taskStepObject) {
//        this.taskStepObject = taskStepObject;
//    }

    private BatchExecReq request;

//    public BatchExecReq getRequest() {
//        return request;
//    }
//
//    public void setRequest(BatchExecReq request) {
//        this.request = request;
//    }

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 异步执行类
     */
    public BatchExecResp doProcess(BatchExecReq request) {
        BatchExecResp obj = new BatchExecResp();
        obj.setRtnCode(CommonConstant.RTN_CODE_SUCCESS);

        log.info("线程调用参数：" + ConvertToUtil.printObjectInfo(request, new ArrayList<>()));
        return obj;
    }

    public String getRunningTime() {
        String startTime = ConvertToUtil.getRunningTime();
        this.taskStartTime = Long.parseLong(startTime);
        return startTime;
    }

    public String getRunningTime(String startTime) {
        String endTime = ConvertToUtil.getRunningTime(startTime);
        this.taskEndTime = Long.parseLong(ConvertToUtil.getRunningTime());
        return endTime;
    }

    public String getStepRunningTime() {
        String startTime = ConvertToUtil.getRunningTime();
        this.taskStepStartTime = Long.parseLong(startTime);
        return startTime;
    }

    public String getStepRunningTime(String startTime) {
        String endTime = ConvertToUtil.getRunningTime(startTime);
        this.taskStepEndTime = Long.parseLong(ConvertToUtil.getRunningTime());
        return endTime;
    }

    @Override
    public BatchExecResp call() {
        return null;
    }

    /**
     * 回写 执行结果
     */
    public abstract BatchExecResp writeBackResult(BatchExecResp request,E taskObject);

    /**
     * 回写子任务 执行结果
     */
    public abstract BatchExecResp writeBackStepResult(BatchExecResp request,R taskStepObject) throws TaskStepExecException;


    public abstract void writeBackResultForRedis(E taskExec) throws TaskExecException;

    /**
     * 调用执行方法
     */
    public abstract BatchExecResp execute(BatchExecReq request) throws TaskStepExecException;


    /**
     * 初始化 参数
     */
    public void initParamter(E taskExec, List<R> taskStepExecList, String ip, String port, String path) {
        this.setTaskObject(taskExec);
        this.setTaskStepListObject(taskStepExecList);
        this.setIp(ip);
        this.setPort(port);
        this.setPath(path);
    }

    /**
     * 打印运行信息(头部)
     *
     * @param className 执行类名称
     * @param request   请求对象
     */
    protected final String pringRunningInfo(String className, BatchExecReq request) {
        //String defaultMsg = "BatchServiceBase -> pringRunningInfo exception!";
        this.request = request;
        return (className + " -> execute running   " + "跑批任务对象：" + ConvertToUtil.printObjectInfo(request, new ArrayList<>(Arrays.asList(
                        "taskGroup",
                        "taskId",
                        "taskExecId",
                        "execOrder",
                        "connectTimeout",
                        "executeTimeout",
                        "taskDate",
                        "sliceServiceClass"
                ))
                //{{
                //    add("taskGroup"); //task_group 清算组代码
                //    add("taskId"); // task_id 任务ID
                //    add("taskExecId"); // task_execid 批量任务执行ID
                //    add("execOrder"); // exec_order 执行优先级
                //    add("connectTimeout"); // proc_connect_timeout
                //    add("executeTimeout"); // proc_execute_timeout
                //    add("taskDate"); // task_date 清算业务日期（yyyyMMdd）
                //    add("sliceServiceClass"); // slice_service_class 分片任务服务处理类
                //}}
        ) + " " + "--------------------------------------------------------");
    }

    /**
     * 打印运行信息(底部)
     *
     * @param className   执行类名称
     * @param request     请求对象
     * @param consumeTime 消耗时间
     */
    protected final String pringRunningInfo(String className, BatchExecReq request, String consumeTime) {

        return (className + " -> execute 耗时:" + (consumeTime) + ConvertToUtil.printObjectInfo(request, new ArrayList<>()) + " --------------------------------------------------------");

    }

    /**
     * 打印子任务运行信息(头部)
     *
     * @param className    执行类名称
     * @param taskStepExec 请求对象
     */
    protected final String pringRunningInfo(String className, R taskStepExec) {
        //String defaultMsg = "BatchServiceBase -> pringRunningInfo exception!";

        return (className + " -> execute running   " + "跑批子任务对象：" + ConvertToUtil.printObjectInfo(taskStepExec, new ArrayList<>()) + " " + " --------------------------------------------------------");
    }

    /**
     * 打印运行信息(底部)
     *
     * @param className    执行类名称
     * @param taskStepExec 请求对象
     * @param consumeTime  消耗时间
     */
    protected final String pringRunningInfo(String className, R taskStepExec, String consumeTime) {

        return (className + " -> 子任务 execute 耗时:" + (consumeTime) + ConvertToUtil.printObjectInfo(taskStepExec, new ArrayList<>()) + " --------------------------------------------------------");

    }

//    /**
//     * 判断当前线程池 是否可执行
//     *
//     */
//    protected boolean checkThreadPool() {
//        int idleCount = BatchThreadPool.getIdleThreadCount();
//
//        log.info("可执行线程数:[{}]", idleCount);
//        if (idleCount <= 0) {
//            //休眠5秒钟，如果5秒后线程池仍满，则退出任务调度
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            if (BatchThreadPool.getIdleThreadCount() <= 0) {
//                return false;
//            }
//        }
//        return true;
//    }


}
